faceBookCredits = function($)
{
  init = function()
  {
    $(".buyMessages").click(buyMessages);
    $("#messageBuy").click(showBuyDialog);
  }, showBuyDialog = function(jsEvent)
  {
    var xPos = $(window).width()/2 - 300;
    $("#messageBuyDialog").dialog({
      minWidth : 600,
      title : 'Buy Messages',
      dialogClass : "dialogWithDropShadow",
      modal : true,
      position :
        [ jsEvent.clientX - 150, jsEvent.clientY - 150 ]
    });
  }, buyMessages = function()
  {
    var messageCount = $(this).attr("messageCount");
    var obj = {
      method : 'pay',
      order_info : {
        customMessages : messageCount
      },
      action : 'buy_item',
      dev_purchase_params : {
        'oscif' : true
      }
    };

    FB.ui(obj, updateMessageCount);
  }, updateMessageCount = function()
  {
    $.ajax({
      url : "/getMessageCount",
      data : {
        accessToken : FB._authResponse.accessToken,
        userid : faceBook.getUser().id
      },
      dataType : "json",
      success : function(data)
      {
        $("#currentMessageCount").html(data.count);
      }
    });
  };

  return {
    init : init,
    updateMessageCount : updateMessageCount,
    showBuyDialog : showBuyDialog
  };
}(jQuery);

$(function()
{
  faceBookCredits.init();
});